/*  Neu - Feed Aggregator.
 *  Copyright (C) 2009  Joshua Montgomery
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details. 
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.neu.config;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URI;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import com.neu.exceptions.ImproperConfigFile;
import com.neu.sql.DBStruct;

/**Configuration for the moniter.
 * 
 * @author Joshua Montgomery
 *
 */
public class Config {
	    
    /**Keeps track of the configuration values for the bot.*/
    private static Map<String,Object> configMap = 
    	Collections.synchronizedMap(new HashMap<String,Object>(4,1));
    
    
    /**Configures the config class based on the config file.
     * 
     * @param filePath The path of the config file.
     * @throws  If the config file cannot be found.
     * @throws ImproperConfigFile If the config file is not configured properly. 
     */
    public static void configure(String filePath) throws FileNotFoundException, ImproperConfigFile
    {
    	File file = new File(filePath);
    	configure(file);  
    }
    
    /**Configures the config class based on the config file.
     * 
     * @param filePath The path of the config file.
     * @throws  If the config file cannot be found.
     * @throws ImproperConfigFile If the config file is not configured properly. 
     */
    public static void configure(URI filePath) throws FileNotFoundException, ImproperConfigFile
    {
    	File file = new File(filePath);
    	configure(file);  
    }
    
    /**Configures the config class based on the config file.
     * 
     * @param file The config file.
     * @throws  If the config file cannot be found.
     * @throws ImproperConfigFile If the config file is not configured properly. 
     */
    public static void configure(File file) throws FileNotFoundException, ImproperConfigFile
    {
    	BufferedReader bufRead = new BufferedReader( new FileReader(file));
    	String line = "";
    	
    	
    	configMap.put("debugMode", null);//should hold boolean
    	    	    	
    	//load the the config which actual values from the config file
    	try {
			while((line = bufRead.readLine()) != null)
			{
				if(!line.equals("") && line.charAt(0) != '#' && line.charAt(0) != ' ')
				{
					String key = line.substring(0,line.indexOf('=')).trim();
					String data = line.substring(line.indexOf('=') + 1).trim();
					
					if(configMap.containsKey(key))
					{
						//Check if its a database config
						if(key.equals("DATABASE NAME HERE"))
						{
							//Database Type
							int x = data.indexOf(',');							
							((DBStruct)configMap.get(key)).type = data.substring(0, x);
							data = data.substring(x+1);
							
							//Database URL
							x = data.indexOf(',');
							((DBStruct)configMap.get(key)).URL = data.substring(0, x);
							data = data.substring(x+1);
							
							//Database Name
							x = data.indexOf(',');
							((DBStruct)configMap.get(key)).name = data.substring(0, x);
							data = data.substring(x+1);
							
							//Database User Name
							x = data.indexOf(',');
							((DBStruct)configMap.get(key)).username = data.substring(0, x);
							data = data.substring(x+1);
							
							//Database Password							
							((DBStruct)configMap.get(key)).password = data;							
						}
						else						
							configMap.put(key, data);						
					}
					else			
						throw new ImproperConfigFile("The config file is not configured properly." +
													 "The value '" + key + "' should not exist as a variable in the config file.");					
				}
			}
		} catch (IOException e) {			
			e.printStackTrace();
			throw new ImproperConfigFile("The bot config file is not configured properly.");
		}  
		
		try {
			bufRead.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
    }
    
    /**Returns boolean based on whether or not to run the logger in debug mode.
     * 
     * @return True or False
     */
    public static boolean isDebugModeEnabled()
    {
    	if(configMap.get("debugMode") != null)
    		return Boolean.parseBoolean((String)configMap.get("debugMode"));
    	return false;
    }
       
    /**Returns the database type associated with the database reference.
     * 
     *@param dbRef The reference for the database to use.
     *@return The Database type. Returns null if the dbRef does not exist.
     */
    public static String getDatabaseType(String dbRef)
    {    	
    	return ((DBStruct)configMap.get(dbRef)).type;    	
    }
    
    /**Returns the database name associated with the database reference.
     * 
     *@param dbRef The reference for the database to use.
     *@return The Database name. Returns null if the dbRef does not exist.
     */
    public static String getDatabaseName(String dbRef)
    {
    	return ((DBStruct)configMap.get(dbRef)).name;
    }
    
    /**Returns the database user name associated with the database reference.
     * 
     *@param dbRef The reference for the database to use.
     *@return The Database user name. Returns null if the dbRef does not exist.
     */
    public static String getDatabaseUserName(String dbRef)
    {
    	return ((DBStruct)configMap.get(dbRef)).username;
    }
    
    /**Returns the database name associated with the database reference.
     * 
     *@param dbRef The reference for the database to use.
     *@return The Database password. Returns null if the dbRef does not exist.
     */
    public static String getDatabasePassword(String dbRef)
    {
    	return ((DBStruct)configMap.get(dbRef)).password;
    }
      
    /**Returns the database URL associated with the database reference.
     * 
     *@param dbRef The reference for the database to use.
     *@return The Database URL. Returns null if the dbRef does not exist.
     */
    public static String getDatabaseURL(String dbRef)
    {
    	return ((DBStruct)configMap.get(dbRef)).URL;
    }  
}
